<?php
defined('IN_YuLin') || exit('NO PERMIT!'); 
$page = 'statistic/lottery';

$profitTab = Table('statistic_lottery');
$lottTab   = Table('lottery');
$sql       = 'SELECT `id`, `bet_amount`, `prize_amount`, `winner_amount`, `bet_count`, `bet_max_nums`, `bet_min_nums`, `lott_id`,
            	`bet_max_money`, `bet_min_money`, `datetime` FROM ' . $profitTab;
$where     = ' WHERE 1 ';
$ord       = '';
$searchBtime = $_GET['btime'];
$searchEtime = $_GET['etime'];

$lott_id   = NoBadStr($_GET['lott_id']);
$btime     = NoBadStr($searchBtime);
$etime     = NoBadStr($searchEtime);
$orderby   = NoBadStr($_GET['orderby']);
switch ($orderby){
    case 'bet_asc':
        $ord = ' ORDER BY bet_amount ASC';
        break;
    case 'bet_desc':
        $ord = ' ORDER BY bet_amount DESC';
        break;
    case 'prize_asc':
        $ord = ' ORDER BY prize_amount ASC';
        break;
    case 'prize_desc':
        $ord = ' ORDER BY prize_amount DESC';
        break;
    case 'betnums_asc':
        $ord = ' ORDER BY bet_max_nums ASC';
        break;
    case 'betnums_desc':
        $ord = ' ORDER BY bet_max_nums DESC';
        break;
    case 'prize_asc':
        $ord = ' ORDER BY prize_amount ASC';
        break;
    case 'prize_desc':
        $ord = ' ORDER BY prize_amount DESC';
        break;
    default :
        $ord = ' ORDER BY datetime DESC';
        break;
}
if ($lott_id)  $where .= ' AND lott_id="' . $lott_id . '"';
if ($btime)    $where .= ' AND datetime >= ' . strtotime($btime);
if ($etime)    $where .= ' AND datetime <= ' . strtotime($etime);
$sql .= $where . $ord;
$tmp = $db->getrow($sql);
if(count($tmp) > 0){
    $nums     = max(1,intval($_GET['page']));
    $size     = 20;
    $limit    = ' LIMIT '.($nums - 1) * $size.','.$size;
    $url      = '?m=' . $m . '&c=' . $c . '&lott_id=' . $lott_id  .'&btime='. $btime . '&etime=' . $etime . '&orderby' . $orderby;
    $data     = $db->getall($sql . $limit);
    $multi    = Multi($url, $nums, $tmp['p'], $size);
}
// 载入所有彩种信息，并IdKey化
$sql      = 'SELECT id, title FROM ' . $lottTab . ' WHERE status = 1';
$lottList = $db->getall($sql);
$lottIdKey = array();
foreach ($lottList as $lk => $lv){
    $lottIdKey[$lv['id']] = $lv;
}
$tpl->display($page);
